package com.company.cloud.user.modules.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.company.modules.api.response.ThirdPlatformConfigModel;
import com.company.cloud.sdk.weixin.common.config.WxApiConfig;
import com.company.cloud.sdk.weixin.common.exception.WxException;
import com.company.cloud.sdk.weixin.mini.WxMiniApi;
import com.company.cloud.sdk.weixin.mini.model.Code2SessionModel;
import com.company.cloud.sdk.weixin.mini.model.Code2SessionResp;
import com.company.cloud.sdk.weixin.mini.model.DecryptDataModel;
import com.company.cloud.user.modules.app.models.ThirdUserEntity;
import com.company.cloud.user.modules.app.service.ThirdUserApiService;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.common.server.constant.LoginThirdChannelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ThirdUserApiServiceImpl implements ThirdUserApiService {

    @Autowired
    private UserCompanyService userCompanyService;

    @Override
    public ThirdUserEntity loginThird(LoginThirdChannelEnum channel, String companyNo, String code) {
        ThirdPlatformConfigModel thirdConfig = userCompanyService.getThirdConfig(companyNo, channel);
        if (LoginThirdChannelEnum.WxMini_User == channel) {
            try {
                WxApiConfig config = BeanUtil.fillBeanWithMapIgnoreCase(thirdConfig.getThirdPlatformConfig(), new WxApiConfig(), false);
                Code2SessionModel wxPara = new Code2SessionModel();
                wxPara.setJs_code(code);
                wxPara.setAppid(config.getAppid());
                wxPara.setSecret(config.getSecret());
                Code2SessionResp wxResult = WxMiniApi.code2Session(wxPara);
                return new ThirdUserEntity(wxResult.getOpenid(), wxResult.getSession_key());
            } catch (WxException wxe) {
                throw new BusinessException(wxe.getCode(), wxe.getMsg());
            }
        } else if (LoginThirdChannelEnum.WxApp_User == channel) {
            return new ThirdUserEntity(code, code);
        } else {
            throw new BusinessException(BaseReturnEnum.Support_Not_Service);
        }
    }

    @Override
    public JSONObject decryptData(LoginThirdChannelEnum channel, String encryptedData, String iv, String sessionKey) {
        if (LoginThirdChannelEnum.WxMini_User == channel) {
            System.out.println(StrUtil.format("encryptedData={},iv={},sessionKey={}", encryptedData, iv, sessionKey));
            try {
                DecryptDataModel entity = new DecryptDataModel();
                entity.setEncryptedData(encryptedData);
                entity.setIv(iv);
                entity.setSessionKey(sessionKey);
                return WxMiniApi.decryptData(entity);
            } catch (WxException e) {
                throw new BusinessException(e.getCode(), e.getMsg());
            } catch (Exception e1) {
                throw new BusinessException(BaseReturnEnum.Call_Third_Error);
            }
        } else {
            throw new BusinessException(BaseReturnEnum.Support_Not_Service);
        }
    }


}
